﻿using System;
using System.Data;
using System.Collections.Generic;
using Slja.Model;
using Slja.Utility;

namespace Slja.BLL
{
    /// <summary>
    /// OrderBLL
    /// </summary>
    public partial class OrderBLL
    {
        private readonly Slja.DAL.OrderDAL dal = new Slja.DAL.OrderDAL();
        public OrderBLL()
        { }
        public string GetMaxPrjID(DateTime dtm, string ProductType)
        {
            return dal.GetMaxPrjID(dtm, ProductType);
        }
        public void SubmitWithLog(OrderSubmitModel submitModel, OrderLogModel logModel)
        {
            dal.SubmitWithLog(submitModel, logModel);
        }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            return dal.Exists(ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Slja.Model.OrderModel model)
        {
            return dal.Add(model);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int AddOrderWithLog(Slja.Model.OrderModel model,OrderLogModel log)
        {
            //添加订单
            int Orderid = dal.Add(model);
            log.OrderID = Orderid;
            //添加日志
            OrderLogBLL lbll = new OrderLogBLL();
            lbll.Add(log);
            return Orderid;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Slja.Model.OrderModel model)
        {
            return dal.Update(model);
        }
        public bool UpdateDesignMemo(Slja.Model.OrderModel model)
        {
            return dal.UpdateDesignMemo(model);
        }
        public bool UpdateSingelField(int OrderID, KeyValuePair<OrderField, string> UpdateFieldVal)
        {
            return dal.UpdateSingelField(OrderID,UpdateFieldVal);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            return dal.Delete(ID);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            return dal.DeleteList(IDlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Slja.Model.OrderModel GetModel(int ID)
        {
            return dal.GetModel(ID);
        }
        public Slja.Model.OrderExtModel GetModelExt(int ID)
        {
            //return dal.GetModelExt(ID);
            return this.GetModelExt(ID, false);
        }
        public Slja.Model.OrderExtModel GetModelExt(int ID,bool WithTeamAndFiles)
        {
            OrderExtModel model = dal.GetModelExt(ID);
            if (WithTeamAndFiles)
            {
                //工作组赋值
                OrderWorkTeamBLL teambll = new OrderWorkTeamBLL();
                model.WorkTeam = teambll.GetWorkersList(model.ID);
                //文件赋值
                OrderFilesBLL filebll = new OrderFilesBLL();
                model.FileList = filebll.GetOrderFileModelList(model.ID);
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Slja.Model.OrderModel> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Slja.Model.OrderExtModel> GetModelExtList(string strWhere)
        {
            DataSet ds = dal.GetOrderExtList(strWhere);
            return DataTableToExtModelList(ds.Tables[0]);
        }  
        /// <summary>
        /// 获得分页数据列表
        /// </summary>
        public List<Slja.Model.OrderExtModel> GetModelExtList(string strWhere,string strOrderby,int pageSize,int curPage,out int recordCount)
        {
            DataSet ds = dal.GetListByPage(strWhere, strOrderby,pageSize,curPage,out recordCount);
            return DataTableToExtModelList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Slja.Model.OrderModel> DataTableToList(DataTable dt)
        {
            List<Slja.Model.OrderModel> modelList = new List<Slja.Model.OrderModel>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Slja.Model.OrderModel model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Slja.Model.OrderModel();                    
                    dal.AssignModel(dt.Rows[n],model);
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        private List<Slja.Model.OrderExtModel> DataTableToExtModelList(DataTable dt)
        {
            List<Slja.Model.OrderExtModel> modelList = new List<Slja.Model.OrderExtModel>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Slja.Model.OrderExtModel model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Slja.Model.OrderExtModel();
                    dal.AssignExtModel(dt.Rows[n], model);                   
                    modelList.Add(model);
                }
            }
            return modelList;
        }     

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        //{
        //    return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        //}
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

